package com.baimifan.renovation.publicfrag;


import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.DisplayMetrics;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.baimifan.renovation.R;
import com.baimifan.renovation.adapter.TaskAdapter;
import com.baimifan.renovation.manager.HomeClassifyTaskManager;

import java.lang.ref.WeakReference;

public class TaskFragment extends Fragment {
    private Float w, h, widthPixels, heightPixels;
    private RecyclerView recyclerView;
    private HomeClassifyTaskManager taskManager;
    private TaskAdapter taskAdapter;

    public static TaskFragment newInstance(String cId) {
        TaskFragment fragment = new TaskFragment();
        Bundle args = new Bundle();
        args.putString("cId", cId);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_task, container, false);
        init(view);
        getDisplay();
        return view;
    }

    @Override
    public void setUserVisibleHint(boolean isVisibleToUser) {
        super.setUserVisibleHint(isVisibleToUser);
        if (getUserVisibleHint()) {
            if (getArguments() != null) {
                taskManager = new HomeClassifyTaskManager(new MyHandler(this));
                String cId = getArguments().getString("cId");
                taskManager.getTask(cId);
            }
        }
    }

    private void init (View view) {
        recyclerView = (RecyclerView) view.findViewById(R.id.task_recyclerview);
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getActivity());
        linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        recyclerView.setLayoutManager(linearLayoutManager);
    }

    private void getDisplay() {
        DisplayMetrics metrics = new DisplayMetrics();
        getActivity().getWindowManager().getDefaultDisplay().getMetrics(metrics);
        widthPixels = Float.valueOf(metrics.widthPixels);
        heightPixels = Float.valueOf(metrics.heightPixels);
        w = widthPixels / 360;
        h = heightPixels / 640;
    }

    private class MyHandler extends Handler {
        WeakReference<TaskFragment> mtask;

        public MyHandler (TaskFragment taskFragment) {
            mtask = new WeakReference<TaskFragment>(taskFragment);
        }
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            TaskFragment task = mtask.get();
            switch (msg.what) {
                case 10:
                    taskAdapter = new TaskAdapter(taskManager.gettasks(), w, h);
                    recyclerView.setAdapter(taskAdapter);
                    taskAdapter.notifyDataSetChanged();
                    break;
            }
        }
    }
}
